package com.example.flowermall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flowermall.entity.*;
import com.example.flowermall.service.IProductImgService;
import com.example.flowermall.service.IProductService;
import com.example.flowermall.service.IShoppingCartService;
import com.example.flowermall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  购物车
 * </p>
 *
 * @author xiaoman
 * @since 2023-07-10
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    IShoppingCartService shoppingCartService;
    @Autowired
    IProductService productService;
    @Autowired
    IProductImgService productImgService;
    @Autowired
    IUserService userService;
    @GetMapping("/list")
    public Result list(
                       @RequestParam(value = "pageNo",required = false)Long pageNo,
                       @RequestParam(value = "pageSize",required = false)Long pageSize){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        Map<String,Object> data=new HashMap<>();
        Page<ShoppingCart> page=new Page<>(pageNo,pageSize);
        shoppingCartService.page(page, queryWrapper);
        List<ShoppingCart> records = page.getRecords();
        for (ShoppingCart record : records) {
            Integer productId = record.getProductId();
            Integer userId = record.getUserId();
            User userById = userService.getUserById(userId);
            Product goodsById = productService.getGoodsById(productId);
            record.setProducts(goodsById);
            record.setUserName(userById.getUsername());
        }
        Result result=Result.success();
        data.put("total",page.getTotal());
        data.put("rows",records);
        result.put("data",data);
        return result;
    }
    @GetMapping("/lists")
    public Result list(@RequestParam(value = "userId" ,required = false)String userId){
        List<ShoppingCart> shoppingCartList = shoppingCartService.select(userId);
        for (ShoppingCart record : shoppingCartList) {
            Integer productId = record.getProductId();
            Integer userId1 = record.getUserId();
            User userById = userService.getUserById(userId1);
            Product goodsById = productService.getGoodsById(productId);
            record.setProducts(goodsById);
            record.setUserName(userById.getUsername());
        }
        Result result=Result.success();
        result.put("cartList",shoppingCartList);
        return  result;
    }
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.add(shoppingCart);
        return Result.success("添加成功");

    }
    @GetMapping("/{id}")
    public Result getShoppingCartById(@PathVariable("id")Integer id){
        ShoppingCart shoppingCart = shoppingCartService.getShoppingCartById(id);
        Result result=Result.success();
        result.put("data",shoppingCart);
        return  result;
    }
    @PutMapping
    public Result updateCart(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.updateCart(shoppingCart);
        return Result.success("修改成功");
    }
    @DeleteMapping("/{id}")
    public Result deleteCart(@PathVariable("id")Integer id){
        shoppingCartService.deleteCart(id);
        return Result.success("删除成功");
    }

    @DeleteMapping("/delete")
    public Result deleteCarts(@RequestBody List<Integer> basketIds){
        for (int i=0;i<basketIds.size();i++){
            shoppingCartService.deleteCart(basketIds.get(i));
        }
        return Result.success("删除照片");
    }

    @GetMapping("/cart/{userId}")
    public Result getCartList(@PathVariable("userId") Integer userId){
        List<ShoppingCart> cartByUserId = shoppingCartService.getCartByUserId(userId);
        for(int i=0;i<cartByUserId.size();i++){
            Product goodsById = productService.getGoodsById(cartByUserId.get(i).getProductId());
            ProductImg productImg = productImgService.getByProductId(cartByUserId.get(i).getProductId());
            if(productImg!=null){
                goodsById.setProductUrl(productImg.getImgUrl());
            }
            cartByUserId.get(i).setProducts(goodsById);
        }
        return Result.success(cartByUserId);
    }

    @GetMapping("/productId/{productId}")
    public Result getProductById(@PathVariable( "productId")Integer productId) {
        ProductImg byProductId = productImgService.getByProductId(productId);
        Product product = productService.getGoodsById(productId);
        if (byProductId != null) {
            product.setProductUrl(byProductId.getImgUrl());
        }
        List<ShoppingCart> shoppingCartList= new ArrayList<>();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setCartNum(1);
        shoppingCart.setProducts(product);
        shoppingCartList.add(shoppingCart);
        double v = Double.parseDouble(String.valueOf(product.getPrice()));
        Result result = Result.success();
        result.put("data", shoppingCartList);
        result.put("totalCount", 1);
        result.put("total", product.getPrice());
        result.put("transfee", 5.00);
        result.put("actualTotal", v + 5.00);
        return result;
    }

    @GetMapping("/ids/{ids}")
    public Result getShoppingCartByIds(@PathVariable("ids") List<Integer> ids){
        List<ShoppingCart> shoppingCartList=new ArrayList<>();
        Integer count=0;
        Double total=0.0;
        for(int i=0;i<ids.size();i++){
            ShoppingCart shoppingCart = shoppingCartService.getShoppingCartById(ids.get(i));
            Product goodsById = productService.getGoodsById(shoppingCart.getProductId());
            ProductImg productImg = productImgService.getByProductId(goodsById.getProductId());
            if(productImg!=null){
                goodsById.setProductUrl(productImg.getImgUrl());
            }
            shoppingCart.setProducts(goodsById);
            count+=shoppingCart.getCartNum();
            total=total+shoppingCart.getCartNum()*Double.parseDouble(String.valueOf(shoppingCart.getProductPrice()));
            shoppingCartList.add(shoppingCart);
        }
        Integer transfee=0;
        if(count<10){
            transfee=5;
        }
        Result result=Result.success();
        result.put("data",shoppingCartList);
        result.put("totalCount",count);
        result.put("total",total);
        result.put("transfee",transfee);
        result.put("actualTotal",total+transfee);
        return  result;
    }

}
